package com.its.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.its.commons.redisDao.RedisDao;
import com.its.domain.BaseResult;

import com.its.domain.CodeEnum;
import com.its.gym.exception.BusException;
import com.its.member.feign.SmsFeign;
import com.its.member.mapper.MemberMapper;
import com.its.member.pojo.Member;
import com.its.member.service.MemberService;
import com.its.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService{
    //依赖注入短信服务的Feign接口
    @Autowired
    private SmsFeign smsFeign;

    //依赖注入RedisDao
    @Autowired
    @Qualifier("stringRedisDao")
    private RedisDao<String> redisDao;

    //依赖注入Mapper层属性
    @Autowired
    private MemberMapper memberMapper;

    /*//账号密码登录
    @Override
    public void memberLoginByMemberAccountAndMemberPasswordService(Member member) {
        //校验用户名
        if (!StringUtils.hasText(String.valueOf(member.getMemberAccount()))) {
            throw new BusException(CodeEnum.LOGIN_NAME_PASSWORD_NAME_IS_NULL.getCode(), CodeEnum.LOGIN_NAME_PASSWORD_NAME_IS_NULL.getMessage());
        }
        //校验密码
        if (!StringUtils.hasText(member.getMemberPassword())) {
            throw new BusException(CodeEnum.LOGIN_NAME_PASSWORD_PASSWORD_IS_NULL.getCode(), CodeEnum.LOGIN_NAME_PASSWORD_PASSWORD_IS_NULL.getMessage());
        }
        //根据用户名查询账户信息
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("memberAccount", member.getMemberAccount());
        queryWrapper.or();
        queryWrapper.eq("memberPhone", member.getMemberPhone());
        Member member1 = memberMapper.selectOne(queryWrapper);
        //校验密码
        if (member1 == null || !Md5Util.verify(member.getMemberPassword(), member1.getMemberPassword())) {
            throw new BusException(CodeEnum.LOGIN_NAME_PASSWORD_ERROR.getCode(), CodeEnum.LOGIN_NAME_PASSWORD_ERROR.getMessage());
        }
    }*/

    //发送登录验证码
    @Override
    public BaseResult sendMemberLoginCode(String memberPhone) {
        //a. 生成四位随机验证码
        String code = RandomUtil.buildCheckCode(4);
        //b. 远程调用短信服务发送验证
        System.out.println("【sendMemberLoginCode】开始执行！");
        System.out.println("调用 Feign 发送短信！");
        BaseResult baseResult = smsFeign.sendSmsCode(memberPhone, code);
        System.out.println("【Feign 返回结果】：" + baseResult);
        //c. 缓存注册验证码到redis中(缓存5分钟)
        if (baseResult.getCode() == 200) {
            redisDao.set("member:login:" + memberPhone, code, 5, TimeUnit.MINUTES);
            System.out.println("用户登录验证码为:"+code);
        }
        return BaseResult.ok();
    }

    //手机号验证码登录
    @Override
    public void checkMemberLoginCodeService(String memberPhone, String code) {
        //1. 校验验证码
        String sysCode = redisDao.get("member:login:" + memberPhone);
        if (sysCode == null || !sysCode.equals(code)) {
            throw new BusException(CodeEnum.REGISTER_CODE_ERROR.getCode(), CodeEnum.REGISTER_CODE_ERROR.getMessage());
        }
        //2.根据手机号查询用户信息
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_phone", memberPhone);
        Member member = memberMapper.selectOne(queryWrapper);
        if (member == null) {
            throw new BusException(CodeEnum.LOGIN_NOPHONE_ERROR.getCode(), CodeEnum.LOGIN_NOPHONE_ERROR.getMessage());
        }
    }
}
